package com.zouqone.smc.common.utils.vo;

import java.util.Iterator;

import net.sf.json.JSONObject;

import com.zouqone.smc.common.utils.StringUtils;

/**
 * 
 * @author zouqh
 * 实体管理查询对象
 */
public class Refer {

	/**
	 * 关联类型
	 */
	private String type;
	
	/**
	 * 关联页面对象
	 */
	private String dialog;
	
	/**
	 * 关联关系
	 * 结构： 目标=源 ，逗号分隔
	 * 如： "article=article,links=links" 
	 */
	private String map;
	
	private String iframes;
	
	public Refer() {
		// TODO Auto-generated constructor stub
		this.type = "0";
	}

	
	public String getIframes() {
		return iframes;
	}


	public void setIframes(String iframes) {
		this.iframes = iframes;
	}


	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}


	public String getDialog() {
		return dialog;
	}


	public void setDialog(String dialog) {
		this.dialog = dialog;
	}
	
	
	public String getMap() {
		return map;
	}


	public void setMap(String map) {
		this.map = map;
	}

	public String getReferJsonFormMap(){
		return getReferJsonFormMap(this.map);
	}

	/**
	 * refer Map to String
	 * @param refermap
	 * @return
	 */
	public String getReferJsonFormMap(String refermap){
		if(StringUtils.isEmpty(refermap)){
			return null;
		}
		String referjson = null;
		String[] referItem = refermap.split(",");
		JSONObject jsonObject = new JSONObject();
		for (String item : referItem) {
			String[] keyValue = item.split(",");
			jsonObject.put(keyValue[0], keyValue[1]);
		}
		referjson = jsonObject.toString();
		return referjson;
	}
	
	
	@SuppressWarnings("rawtypes")
	public String getReferMapFormJson(String referjson){
		if(StringUtils.isEmpty(referjson)){
			return null;
		}
		StringBuffer refermap = new StringBuffer();
		JSONObject jsonObject = JSONObject.fromObject(referjson);
		int flag = 0;
		for (Iterator iter = jsonObject.keys(); iter.hasNext();) { 
			 String key = (String)iter.next();
			 String value = jsonObject.getString(key);
			 if(flag > 0){
				 refermap.append(",");
			 }
			 refermap.append(key).append("=").append(value);
			 flag++;
		}
		
		return refermap.toString();
	}

}
